'use client'

import { useEffect, useState } from 'react'
import { Supplier } from '@prisma/client'

export function useSuppliers() {
  const [suppliers, setSuppliers] = useState<Supplier[]>([])
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<Error | null>(null)

  useEffect(() => {
    async function fetchSuppliers() {
      try {
        const response = await fetch('/api/suppliers')
        if (!response.ok) {
          throw new Error('获取供应商失败')
        }
        const data = await response.json()
        setSuppliers(data)
      } catch (err) {
        setError(err instanceof Error ? err : new Error('未知错误'))
      } finally {
        setLoading(false)
      }
    }

    fetchSuppliers()
  }, [])

  return { suppliers, loading, error }
} 